package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseTableListAdapter;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.MaterieListOriginal;
import com.isunland.managesystem.entity.MaterieListOriginal.MaterieListContent;
import com.isunland.managesystem.entity.OderSaleContent;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;

public class MaterielListFragment extends BaseListFragment {
  public static final String EXTRA_CONTENT = "com.isunland.managesystem.entity.EXTRA_CONTENT";
  public static final int ADD_MATERIEL = 0;
  public static final int DETAIL_MATERIEL = 1;
  private ArrayList<MaterieListContent> mList;
  //	private MaterieLisAdapter mAdapter;
  private BaseTableListAdapter mAdapter;
  private OderSaleContent mOderSaleContent;

  @Override public void onCreate(Bundle savedInstanceState) {
    mOderSaleContent = (OderSaleContent) getArguments().getSerializable(EXTRA_CONTENT);
    super.onCreate(savedInstanceState);
  }

  @Override public String getUrl() {
    return ApiConst.URL_MATERIE_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("mainId", mOderSaleContent.getId());
    return params;
  }

  @Override public void resolveResponse(String arg0) {
    MaterieListOriginal original = new Gson().fromJson(arg0, MaterieListOriginal.class);
    ArrayList<MaterieListContent> rows = (ArrayList<MaterieListContent>) original.getRows();
    if (mList == null) {
      mList = new ArrayList<>();
    }
    mList.clear();
    mList.addAll(rows);
    setAdapter(mList);
  }

  private void setAdapter(ArrayList<MaterieListContent> mList2) {

    LinkedHashMap<String, String> map = new LinkedHashMap<>();
    map.put("物料名称", "materialName");
    map.put("出库数量", "moutNumber");
    map.put("单价", "moutPrice");
    if (mAdapter == null) {
      mAdapter = new BaseTableListAdapter<>(getActivity(), mList2, map,
          new MaterieListOriginal().new MaterieListContent());
    }
    setListAdapter(mAdapter);
    ((BaseTableListAdapter) getListAdapter()).notifyDataSetChanged();
  }

  //点击跳转到物料详情界面
  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    MaterieListContent mContent = mList.get(position - 1);
    Intent i = new Intent(getActivity(), ShowMaterieDetailActivity.class);
    i.putExtra(ShowMaterieDetailFragment.EXTRA_CONTENT, mContent);
    i.putExtra(ShowMaterieDetailFragment.EXTRA_ORDER_CONTENT, mOderSaleContent);
    startActivityForResult(i, DETAIL_MATERIEL);
    super.onListItemClick(l, v, position, id);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_add, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.menu_item_add:
        if ("new".equals(mOderSaleContent.getMoutbillStatus()) || "abort".equals(
            mOderSaleContent.getMoutbillStatus())) {
          Intent i = new Intent(getActivity(), AddMaterielDetailActivity.class);
          i.putExtra(AddMaterielDetailFragment.EXTRA_CONTENT, mOderSaleContent);
          startActivityForResult(i, ADD_MATERIEL);
        } else {
          ToastUtil.show(R.string.draftOrAbort);
        }

        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  public void refresh() {
    volleyPost();
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == ADD_MATERIEL) {
      volleyPost();
    }
    if (requestCode == DETAIL_MATERIEL) {
      volleyPost();
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  public static MaterielListFragment newInstance(OderSaleContent item) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CONTENT, item);
    MaterielListFragment fragment = new MaterielListFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
